package com.example.rabbitmq.example.spring;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.MessagePropertiesBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;

import java.nio.charset.StandardCharsets;

public class ProducerApp {

    public static void main(String[] args) {
        AbstractApplicationContext context = new AnnotationConfigApplicationContext(RabbitConfig.class);
        final RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);
        final MessageProperties messageProperties = MessagePropertiesBuilder.newInstance()
                .setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN)
                .setContentEncoding(StandardCharsets.UTF_8.name())
                .setHeader("key", "value")
                .build();

        final Message message = MessageBuilder.withBody("您好".getBytes(StandardCharsets.UTF_8))
                .andProperties(messageProperties)
                .build();
        rabbitTemplate.send("ex.anno.fanout", "key.anno", message);
    }
}
